package com.nothing.controller;

import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ResourceBundle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;

import com.nothing.config.StageManager;
import com.nothing.domain.UserFaceGroup;
import com.nothing.service.UserFaceInfoService;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;

/**
 * @author shíQíang㊚
 * @since 2019年5月24日11:28:32
 */

@Controller
public class TakeFaceController implements Initializable {

	@FXML
	public ImageView showImg;

	@FXML
	private Label userId;

	@FXML
	private TextField name;

	@FXML
	private RadioButton rbMale;

	@FXML
	private ToggleGroup gender;

	@FXML
	private RadioButton rbFemale;

	@FXML
	private TextField age;

	@FXML
	private Button saveUser;

	@FXML
	private ChoiceBox<UserFaceGroup> groupOptions;
	@FXML
	private Label showMsg;
	@Lazy
	@Autowired
	private StageManager stageManager;

	private UserFaceInfoService userFaceInfoService;

	private ObjectProperty<Image> faceProperty = new SimpleObjectProperty<Image>();

	public TakeFaceController() {}
 

	public TakeFaceController(Image img, UserFaceInfoService userService) {
		this.faceProperty.set(img);
		this.userFaceInfoService = userService;
	}

	@FXML
	private void saveUser(ActionEvent event) {

		try {
			BufferedImage img = SwingFXUtils.fromFXImage(showImg.getImage(), null);
			saveUser.setDisable(true);
			userFaceInfoService.saveFace(img, name.getText(),
					groupOptions.getSelectionModel().getSelectedItem().getId(), rbMale.isSelected() ? 1 : 0,
					Integer.valueOf(age.getText()));
			showMsg.setText("保存成功!");
			showMsg.setTextFill(Color.WHITE);
			
		} catch (Exception e) {
			showMsg.setText("保存失败!");
			showMsg.setTextFill(Color.RED);
			saveUser.setDisable(false);
			//showMsg.setTextFill(Color.web("#0076a3"));
			e.printStackTrace();
		}

		clearFields();
	}

//	private void saveAlert() {
//
//		Alert alert = new Alert(AlertType.INFORMATION);
//		alert.setTitle("保存");
//		alert.setHeaderText(null);
//		alert.setContentText("保存成功");
//		alert.showAndWait();
//		stage.close();
//	}
//
//	private void saveErrorAlert() {
//
//		Alert alert = new Alert(AlertType.INFORMATION);
//		alert.setTitle("保存");
//		alert.setHeaderText(null);
//		alert.setContentText("保存失败");
//		alert.showAndWait();
//
//	}

	private void clearFields() {
		name.clear();
		rbMale.setSelected(true);
		rbFemale.setSelected(false);
		age.clear();
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		//cbRole.setItems (roles);
		ObservableList<UserFaceGroup> options = FXCollections.observableArrayList(
				UserFaceGroup.builder().id(1L).name("特殊人").build(), UserFaceGroup.builder().id(2L).name("普通人").build());

		groupOptions.setItems(options);

		showImg.imageProperty().bind(faceProperty);

	}

}
